<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css">
    <link rel="stylesheet" href="/layuiadmin/style/login.css">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css">
</head>
<style>
    .layui-tab-title {
        display: flex;
    }
    .layui-tab-title li {
        flex: 1;
    }
    .q-login{background-color: rgb(87, 87, 87);}
    .impowerBox{
        line-height: 1.6;
        margin: 0 auto;
        z-index: 1;
        padding: 15px;
        text-align: center;
    }
    .title{text-align: center;font-size: 20px;color: #fff!important;}
    .impowerBox .status.normal{
        padding: 5px 0;
        margin-top: 15px;
        background-color: #393939;
        border-radius: 100px!important;
        -moz-border-radius: 100px;
        -webkit-border-radius: 100px;
        box-shadow: inset 0 5px 10px -5px #191919, 0 1px 0 0 #444;
        -moz-box-shadow: inset 0 5px 10px -5px #191919,0 1px 0 0 #444;
        -webkit-box-shadow: inset 0 5px 10px -5px #191919, 0 1px 0 0 #444;color: #fff!important;
    }
    .normal p{
        font-size: 14px;
        margin: 0;
        line-height: 2.4;
    }
    .wrp_code{
        width: 240px;
        height: 240px;
        padding: 10px;
        margin: auto;
        background-color: #fff;
    }
    .wrp_code img{width: 90%;padding:5%;}
    .refresh{float: right;}
</style>
<body>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>系统登录</h2>
            <p>Grid系统登录</p>
        </div>

        <div class="layui-tab layui-tab-brief" lay-filter="login">
            <ul class="layui-tab-title">
                <li class="layui-this">账号登录</li>
                <li>扫码登录</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">

                    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
                        <div class="layui-form-item">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                                   for="LAY-user-login-username"></label>
                            <input type="text" name="username" id="LAY-user-login-username" lay-verify="required"
                                   placeholder="用户名" class="layui-input" autocomplete="off">
                        </div>
                        <div class="layui-form-item">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                                   for="LAY-user-login-password"></label>
                            <input type="password" name="password" id="LAY-user-login-password" lay-verify="required"
                                   placeholder="密码" class="layui-input" autocomplete="off">
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-col-xs7">
                                    <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                           for="LAY-user-login-vercode"></label>
                                    <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required"
                                           placeholder="图形验证码" class="layui-input" autocomplete="off">
                                </div>
                                <div class="layui-col-xs5">
                                    <div style="margin-left: 10px;">
                                        <img id="vercodeImg" title="点击刷新验证码" alt="验证码"
                                             onclick="this.src='/kaptcha?d='+new Date()*1"
                                             src="/kaptcha?d=1"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登入</button>
                        </div>
                    </div>
                </div>
                <div class="layui-tab-item q-login">
                    <div class="main impowerBox">
                        <div class="loginPanel normalPanel">
                            <div class="title">微信登录<i class="layui-icon layui-icon-refresh-3 refresh"></i></div>
                            <div class="waiting panelContent">
                                <div class="wrp_code" id="qrcode">二维码生成失败,请重试</div>
                                <div class="info">
                                    <div class="status status_browser js_status js_wx_default_tip normal" id="wx_default_tip">
                                        <p class="prompt-msg">请使用微信扫描二维码登录</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-trans layadmin-user-login-footer">
        <p></p>
    </div>
</div>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user', 'element','qrcode'], function () {
        var element = layui.element,$ = layui.$
            , form = layui.form,qrcode = layui.qrcode;
        var token = "123";
        form.render();
        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            $.ajax({
                url: '/login',
                type: "post",
                data: JSON.stringify(obj.field),
                dataType: "json",
                contentType: "application/json", //必须有
                success: function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        top.window.location.href = "/home/index";
                    } else {
                        $("#vercodeImg").click();
                        layer.msg(res.msg);
                    }
                }
            })
        });

        element.on('tab(login)', function (e) {
            console.log(e);
            if(e.index==1){
                makeCode();
            }else {
                if(myTimer!=undefined){
                    clearInterval(myTimer);
                    qrcode.clear();
                }
            }
        });
        var myTimer;
        var checkLogin = function(token){
            $.ajax({
                url:"/verify",
                type:"post",
                data:token,
                success:function(res){
                    console.log(res);
                    switch (res.code) {
                        case 0:
                            clearInterval(myTimer);
                            top.window.location.href = "/home/index";
                            break;
                        case -1:
                            clearInterval(myTimer);
                            $(".prompt-msg").html(res.msg);
                            break;
                        case 1:
                            break;
                        case 2:
                            $(".prompt-msg").html(res.msg);
                            break;
                    }
                }
            })
        };
        var makeCode = function () {
            $.ajax({
                url:"/getToken",
                type:"get",
                dataType:"json",
                success:function (res) {
                    console.log(res);
                    $("#qrcode").empty();
                    qrcode.qrcode(document.getElementById("qrcode"), {
                        width : 200,
                        height : 200,
                        text:window.location.origin+"/mobile/free_login/"+res.data
                    });
                    myTimer = setInterval(function(){ checkLogin(res.data) },2000);
                }
            })
        };
        $(".refresh").click(function(){
            clearInterval(myTimer);
            makeCode();
        })
    });
</script>
</body>
</html>